package com.ajax;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;


@WebServlet("/addServlet")
public class AddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取请求数据
        BufferedReader reader = req.getReader();
        String s = reader.readLine();

        //将字符串转为java对象
        User user = JSON.parseObject(s, User.class);

        //调用service进行新增  boolean flag = userService.add(user);
        boolean flag = true;
        if (flag){
            resp.getWriter().write("success");
        }else{
            resp.getWriter().write("error");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
